package com.supor.aiot.common.helper;


import android.view.View;

import com.android.baseconfig.common.entity.IntervalItemDecoration;
import com.android.baseconfig.recyclerview.ByRecyclerView;
import com.android.baseconfig.recyclerview.decoration.GridSpaceItemDecoration;
import com.android.baseconfig.recyclerview.decoration.SpacesItemDecoration;
import com.supor.aiot.common.R;
import com.supor.aiot.common.ui.view.byview.NeteaseLoadMoreView;
import com.supor.aiot.common.ui.view.byview.NeteaseRefreshHeaderView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


public class RefreshHelper {

    public static void initStaggeredGrid(ByRecyclerView recyclerView, int spanCount, int spacing) {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(spanCount,
                StaggeredGridLayoutManager.VERTICAL));
        // 如果每个item高度一致设置后效率更高
        recyclerView.setHasFixedSize(true);
        //        recyclerView.setItemAnimator(null);
        recyclerView.addItemDecoration(new GridSpaceItemDecoration(spacing));
        recyclerView.setRefreshHeaderView(new NeteaseRefreshHeaderView(recyclerView.getContext()));
        recyclerView.setLoadingMoreView(new NeteaseLoadMoreView(recyclerView.getContext()));
    }

    public static ByRecyclerView initLinear(ByRecyclerView recyclerView, boolean isDivider) {
        return initLinear(recyclerView, isDivider, 0);
    }

    public static ByRecyclerView initLinear(ByRecyclerView recyclerView, boolean isDivider,
                                            int headerNoShowSize) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        //        recyclerView.setItemAnimator(null);
        if (isDivider) {
            recyclerView.addItemDecoration(new SpacesItemDecoration(recyclerView.getContext(),
                    SpacesItemDecoration.VERTICAL, headerNoShowSize).setDrawable(R.drawable.line_gradient_3));
        }
        recyclerView.setRefreshHeaderView(new NeteaseRefreshHeaderView(recyclerView.getContext()));
        recyclerView.setLoadingMoreView(new NeteaseLoadMoreView(recyclerView.getContext()));
        return recyclerView;
    }

    public static ByRecyclerView setDefaultAnimator(ByRecyclerView recyclerView) {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        return recyclerView;
    }

    public static void initGrid(RecyclerView recyclerView, int spanCount, int spacing) {
        initGrid(recyclerView, spanCount, spacing, spacing);
    }

    public static void initGrid(RecyclerView recyclerView, int spanCount, int dividerWidth, int dividerHeight) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(), spanCount);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(new GridItemDecoration(ContextCompat.getColor(recyclerView.getContext(),
                R.color.transparent), dividerWidth, dividerHeight, -1));
    }

    public static void initLinear(RecyclerView recyclerView, int spacing) {
        initLinear(recyclerView, spacing, RecyclerView.VERTICAL);
    }

    public static void initLinear(RecyclerView recyclerView, int spacing, int orientation) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext());
        linearLayoutManager.setOrientation(orientation);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(
                new IntervalItemDecoration(spacing, spacing, false));
    }
}
